<?php
/**
 * 支付
 * Created by PhpStorm.
 * User: qinzhi
 * Date: 2017-08-16
 * Time: 14:14
 */
class Controller_Wwwmall2_Pay extends FController
{

    public function wxAction(){
        global $_F;

        require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
        $options = array(
            'token' => FConfig::get('wechat.wxpay.token'),
            'appid' => FConfig::get('wechat.wxpay.appid'),
            'appsecret' => FConfig::get('wechat.wxpay.appsecret'),
        );
        $weixin = new WeixinChat($options);
        if(FRequest::getString('code') != ''){
            $code = FRequest::getString('code');
            $result = $weixin->getSnsAccessToken($code);
            $openid = $result['openid'];
            if(empty($openid)){
                header("Content-type: text/html; charset=utf-8");
                die('获取用户基本信息失败!');
            }
        }else{
            global $_F;
            $url = 'http://' . $_F['http_host'] . $_F['uri'];
            if($_F['query_string'] != ''){
                $url .= '?' . $_F['query_string'];
            }
            $weixin->redirectGetOauthCode($url);
        }

        $id = FRequest::getRequestInt('oid');

        $ordersT = new FTable('orders','','xhs_slave');
        $order = $ordersT->where(array('auto_id'=>$id))->find();
        if(empty($order) || $order['status'] != 1){
            return $this->error('订单不存在');
        }



        $pay_price = $order['pay_price'];
        $body = $order['body'];
        $body = !empty($body) ? $body : '订单支付';
        $this->assign('price',$pay_price);
        $this->assign('name','1111');
        $this->assign('body','2222');
        $notify_url = 'http://' . $_F['http_host'] . '/pay/notifyUrl';
        $callback_url = 'http://' . $_F['http_host'] . '/order/detail?oid=' . $order['o_id'];

        /*$userT = new FTable('user','','xhs_slave');
        $userDetail = $userT->where(array('uid'=>$order['uid']))->find();
        $result = Service_WxPay::create_order($order['o_id'],$userDetail['openid'],$pay_price*100,$body,$notify_url,$callback_url);*/
        $result = Service_WxPay::create_order($order['o_id'],$openid,$pay_price*100,$body,$notify_url,$callback_url);
        if($result['status'] == 0) FResponse::redirect($result['pay_url']);
        else {
            FLogger::write($result['status'],'wxpay');
            $this->error('支付出错!错误码: ' . $result['status']);
        }
        //$this->display('pay/wx');
    }

    public function getOpenIdAction(){
        require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
        $options = array(
            'token' => FConfig::get('wechat.wxpay.token'),
            'appid' => FConfig::get('wechat.wxpay.appid'),
            'appsecret' => FConfig::get('wechat.wxpay.appsecret'),
        );
        $weixin = new WeixinChat($options);

        if(FRequest::getString('code') != ''){
            $code = FRequest::getString('code');
            $result = $weixin->getSnsAccessToken($code);
            $openid = $result['openid'];
            if(empty($openid)){
                header("Content-type: text/html; charset=utf-8");
                die('获取用户基本信息失败!');
            }
            $refer = urldecode(FRequest::getString('refer'));
            $refer .= '&openid=' . $openid;
            header("Location: $refer");
            exit;
        }else{
            global $_F;
            $url = 'http://' . $_F['http_host'] . $_F['uri'];
            if($_F['query_string'] != ''){
                $url .= '?' . $_F['query_string'];
            }
            $weixin->redirectGetOauthCode($url);
        }
    }

    /**
     * 通知处理
     */
    public function notifyUrlAction(){
        Service_WxPay::notify_url();
    }

    function showMessage($message, $msgType, $url = null)
    {
        echo "<script>alert('{$message}');";
        if ($url) echo "location='{$url}';";
        echo "</script>";
    }
}